package com.stx.openeyes.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stx.openeyes.R;
import com.stx.openeyes.model.HomePicEntity;
import com.stx.openeyes.model.Video;
import com.stx.openeyes.utils.XStringUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MyVideoItemAdapter extends BaseAdapter {
    private static final String TAG = "MyVideoItemAdapter";
    private Context mContext;
    List<Video> mItemList;

    public MyVideoItemAdapter(Context mContext, List<Video> itemList) {
        this.mContext = mContext;
        this.mItemList = itemList;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public Object getItem(int position) {
        return mItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ViewHolder mHolder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Video itemListEntity = mItemList.get(position);
        mHolder = new ViewHolder();

        //设置布局
        convertView = LayoutInflater.from(mContext).inflate(R.layout.video_card_view_item, parent, false);
        if (convertView.getTag() instanceof ViewHolder) {
            mHolder = (ViewHolder) convertView.getTag();
        } else {
            mHolder.imageView = convertView.findViewById(R.id.iv_item);
            mHolder.tvTitle = convertView.findViewById(R.id.tv_title_item);
            mHolder.tvTime = convertView.findViewById(R.id.tv_time_item);
            mHolder.tvTag = convertView.findViewById(R.id.tv_tag_item);
            mHolder.tvVip = convertView.findViewById(R.id.tv_vip_item);
            mHolder.tvProgress = convertView.findViewById(R.id.tv_download_progress);

            convertView.setTag(mHolder);
        }
        //set data
        Glide.with(mContext).load(itemListEntity.getPreviewImg()).into(mHolder.imageView);
        mHolder.tvTitle.setText(itemListEntity.getTitle());
        mHolder.tvTime.setText(itemListEntity.getDurationStr());
        mHolder.tvTag.setText(itemListEntity.getTagStr());
        mHolder.tvProgress.setText(itemListEntity.getDownloanProgress() + "%");
        if (itemListEntity.getType() == 1) {
            mHolder.tvVip.setVisibility(View.VISIBLE);
        } else {
            mHolder.tvVip.setVisibility(View.INVISIBLE);
        }
        if (itemListEntity.getIsDownloan() == 1) {
            mHolder.tvProgress.setVisibility(View.VISIBLE);
        } else {
            mHolder.tvProgress.setVisibility(View.GONE);
        }
        return convertView;

    }

    static class ViewHolder {
        ImageView imageView;
        TextView tvTitle;
        TextView tvTime;
        TextView tvVip;
        TextView tvTag;
        TextView tvProgress;
    }

    static class ViewHolder2 {
        TextView tvTime;
    }
}
